import java.util.HashMap;
import java.util.Map;

public class Solution14 {
    //1027. 最长等差数列
    public int longestArithSeqLength(int[] nums) {
        int n = nums.length;
        Map<Integer,Integer> hash = new HashMap<>();
        int[][] dp = new int[n][n];
        hash.put(nums[0],0);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = 2;
            }
        }
        int ret = 2;
        for (int i = 1; i < n; i++) {   //固定前一个位置
            for (int j = i+1; j < n; j++) {   //第二个位置从前向后遍历
                int a = 2 * nums[i] - nums[j];
                if (hash.containsKey(a)) {
                    dp[i][j] = dp[hash.get(a)][i] + 1;
                    ret = Math.max(ret,dp[i][j]);
                }
            }
            hash.put(nums[i],i);
        }
        return ret;
    }
}
